extends Label

var init_color
var instance_id: int
static var active_instances: Dictionary = {}  # 改为字典，按角色管理
const MAX_INSTANCES = 8  # 限制最大同时显示数量
var base_font_size = 1  # 基础字体大小
var owner_node: Node  # 记录所属节点
var is_critical = false  # 是否为暴击
var critical_font_size_multiplier = 2.0  # 暴击字体大小倍率
var critical_color = Color(1.0, 0.84, 0.0)  # 金色
var critical_outline_color = Color(1.0, 1.0, 1.0)  # 白色描边

func _ready() -> void:
	# 获取所属节点
	owner_node = get_parent()
	
	# 如果该角色没有实例列表，创建一个
	if not active_instances.has(owner_node):
		active_instances[owner_node] = []
	
	# 如果该角色的实例数量超过限制，移除最旧的实例
	if active_instances[owner_node].size() >= MAX_INSTANCES:
		var oldest_instance = active_instances[owner_node].pop_front()
		if is_instance_valid(oldest_instance):
			oldest_instance.queue_free()
	
	# 为每个实例创建独立的LabelSettings
	var new_label_settings = LabelSettings.new()
	new_label_settings.outline_size = 4
	new_label_settings.outline_color = Color(0, 0, 0, 1)
	new_label_settings.font_color = Color.WHITE
	label_settings = new_label_settings
	
	# 添加到该角色的活跃实例列表并分配ID
	active_instances[owner_node].append(self)
	instance_id = active_instances[owner_node].size() - 1

func _exit_tree():
	# 从该角色的活跃实例列表中移除
	if active_instances.has(owner_node):
		active_instances[owner_node].erase(self)
		# 如果该角色没有实例了，清理字典条目
		if active_instances[owner_node].is_empty():
			active_instances.erase(owner_node)

func set_number(number):
	text = "-" + str(number)

func set_color(color):
	# 只改变当前实例的颜色，不影响其他实例
	if label_settings:
		label_settings.font_color = color

func set_string(text_value):
	text = text_value

## 设置暴击模式
func set_critical(critical: bool):
	is_critical = critical
	if is_critical:
		# 暴击时添加"暴击!"前缀
		if not text.begins_with("暴击! "):
			text = "暴击! " + text
		# 设置暴击颜色和描边
		if label_settings:
			label_settings.font_color = critical_color
			label_settings.outline_color = critical_outline_color
			label_settings.outline_size = 6  # 更粗的描边

## 设置初始位置（在_ready之前调用）
func set_initial_position(pos: Vector2):
	position = pos

## 根据对象scale调整文字大小
func set_scale_factor(scale_factor: float):
	if label_settings:
		var font_size = base_font_size * scale_factor
		font_size = 10
		if is_critical:
			font_size *= critical_font_size_multiplier  # 暴击时字体更大
		
		label_settings.font_size = font_size
	position = -self.size / 2
	pivot_offset = self.size / 2
	
	# 检查是否有重叠，如果有则稍微偏移
	var overlap_offset = 0
	if active_instances.has(owner_node):
		for instance in active_instances[owner_node]:
			if is_instance_valid(instance) and instance != self:
				var distance = abs(position.y - instance.position.y)
				if distance < 20:  # 如果距离太近
					overlap_offset += 1
				
	position.y -= overlap_offset
	
	var tween = create_tween().set_parallel(true).set_ease(Tween.EASE_OUT)
	
	# 暴击时的特殊动画
	if is_critical:
		# 更大的初始缩放
		tween.tween_property(self, "scale", Vector2(1.2, 1.2), 0.15).from(Vector2.ZERO)
		# 添加旋转效果
		tween.parallel().tween_property(self, "rotation_degrees", randf_range(-15, 15), 0.2)
		# 更快的上升动画
		var rand_x = randf_range(-8, 8)
		tween.tween_property(self, "position:x", position.x + rand_x, 0.25)
		var rand_y = 20 + instance_id * 8  # 更高的上升
		tween.tween_property(self, "position:y", position.y - rand_y, 0.4)
		# 更快的淡出
		tween.tween_property(self, "scale", Vector2.ZERO, 0.2).set_delay(0.6)
		tween.tween_callback(self.queue_free).set_delay(0.9)
	else:
		# 普通伤害动画
		tween.tween_property(self, "scale", Vector2(0.8, 0.8), 0.2).from(Vector2.ZERO)
		
		# 减少随机水平偏移
		var rand_x = randf_range(-5, 5)
		tween.tween_property(self, "position:x", position.x + rand_x, 0.3)
		
		# 向上飘动距离，不要太高
		var rand_y = 10 + instance_id * 5  # 基础10像素，每个实例额外5像素
		tween.tween_property(self, "position:y", position.y - rand_y, 0.5)
		
		tween.tween_property(self, "scale", Vector2.ZERO, 0.2).set_delay(0.8)
		tween.tween_callback(self.queue_free).set_delay(1.2)

## 重置实例计数器
static func reset_counter():
	active_instances.clear()
